package com.example.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.example.pojo.ValidateCode;
import com.example.service.ValidateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public ValidateCode generateValidateCode() {
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(100, 40, 4, 10);
        String codeValue = circleCaptcha.getCode();
        String imageBase64 = circleCaptcha.getImageBase64();

        String codeKey = UUID.randomUUID().toString().replaceAll("-", "");

        redisTemplate.opsForValue().set("emp:login:"+codeKey,codeValue,5, TimeUnit.MINUTES);

        //TODO 改为session存储图形验证码


        ValidateCode validateCode = new ValidateCode();
        validateCode.setCodeKey(codeKey);
        validateCode.setCodeValue("data:image/png;base64," + imageBase64);




        return validateCode;
    }
}
